<html id="whole">

<head>
<title>Markdown上传工具, only works with 简书</title>
<link rel="shortcut icon" type="image/png" href="sc.png" />	
</head>

<style>

#raw, #result {
	background-color: rgb(199,237,204);
}

#whole {
	background-color: black;
}
</style>
<textarea id="raw" rows="40" cols="80">
</textarea>
<textarea id="result" rows="40" cols="80">
输入:Typora里的markdown raw
</textarea>
<button onclick="myFunction()">Click me</button>
<script>

var IMAGE_PATTERN = /^!\[(.*)\]\((.*)\)$/;
var IMAGE_INDEX = /^image(\d+).*$/;

function MyImage(index, name, url){
	this.index = parseInt(index);
	this.name = name;
	this.url = url;
}

// input: ![image1.png](https://upload-images.jianshu.io/upload_images/2085791-9aa6232b1426df28.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


function getParsedImage(input) {

	if (input.indexOf("https://upload-images.jianshu.io") < 0 ) {
		return;
	} 
	
	var result = IMAGE_PATTERN.exec(input);
	if( result.length != 3){
		return;
	}

	var imageIndex = IMAGE_INDEX.exec(result[1]);
	console.assert(imageIndex.length === 2);
	return new MyImage(imageIndex[1], result[1], result[2]);
}

function getNodeTobeReplaced(current, parent, newPicUrl ){
	return {
		"current": current,
		"parent": parent,
		"newPicUrl": newPicUrl
	};
}

/*

![C:\\Users\\i042416\\AppData\\Local\\youdao\\ynote\\images\\8978F73796284F2A8A7DC3C3C979EABD\\clipboard.png](media/image35.png){width="7.85in" height="0.6604429133858267in"}

*/

function sortByIndex(v1,v2){
		if(v1.index < v2.index ){
			return -1;
		}
		else if(v1.index > v2.index ){
			return 1;
		}
		else return 0;
}

// extract all image tag uploaded by jianshu

/* jianshu image tag:
![image31.png](https://upload-images.jianshu.io/upload_images/2085791-416440df76a7343c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

*/

function extractImage(source,raw,imagePool){
	var splitted = source.split("\n");
	for( var i = 0; i < splitted.length; i++){
		var origin = splitted[i];

		var parsed = getParsedImage(origin);

		// "should be discarded as well
		if( !parsed ) {
			raw.push(origin);
		}
		else{
			imagePool.push(parsed);
		}
	}
	imagePool.sort(sortByIndex);
}

/*
![C:\\Users\\i042416\\AppData\\Local\\youdao\\ynote\\images\\31961301879E442EB726269DD13AFDCE\\clipboard.png](media/image30.png){width="4.241666666666666in" height="1.1583333333333334in"}

*/

var LOCAL_IMAGE_TAG = "(media/image";

function replaceImageTag(raw,imagePool){
	var formatted = "";
	var line = "";
	for( var i = 0; i < raw.length; i++){
		if( raw[i].indexOf(LOCAL_IMAGE_TAG) < 0){
			line = raw[i];
		}
		else{

			console.log("local image tag found: " + raw[i]);
			var currentImage = imagePool.shift();
			line = "![](" + currentImage.url + ")";
		}
		formatted = formatted + "\n" + line;
	}
	return formatted;
}

function myFunction(){
	var source = document.getElementById("raw");
	var html = source.value;
	var rawItem = [];
	var imagePool = [];
	extractImage(html,rawItem, imagePool);
	var formatted = replaceImageTag(rawItem,imagePool);
	var target = document.getElementById("result");
	target.value = addSignature(formatted);
	copyToClipboard(target);
}

function copyToClipboard(node) {
  node.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    alert('Copying text command was ' + msg);
  } catch (err) {
    alert('Oops, unable to copy: ' + msg);
  }
}

function addSignature(raw){

	var jerry = "![](https://upload-images.jianshu.io/upload_images/2085791-c4f58b9813a954cb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)";
	var result = raw  + "要获取更多Jerry的原创文章，请关注公众号\"汪子熙\":";
	result = result + "\n" + jerry;
	return result;
}
</script>
</html>